package com.jdcjk.project.car.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BNaviSettingManager;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.jdcjk.Constant;
import com.jdcjk.MainApp;
import com.jdcjk.R;
import com.jdcjk.UrlAddress;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.base.view.FragmentViewClickListener;
import com.jdcjk.chat.enity.MessageBean;
import com.jdcjk.chat.view.AccountOfflineDialog;
import com.jdcjk.project.car.presenter.CarPresenter;
import com.jdcjk.project.car.view.baidu.BaiDuRealTrackFragment;
import com.jdcjk.project.car.view.gaode.GaoDeRealTrackFragment;
import com.jdcjk.project.main.model.entity.CarListInfo;
import com.jdcjk.project.navigation.view.baidu.BDCarNavigationFindActivity;
import com.jdcjk.project.navigation.view.gaode.GDCarNavigationFindActivity;
import com.jdcjk.util.BaiDuMapUtil;
import com.jdcjk.util.PreferencesUtils;
import com.jdcjk.view.LoadingDialog;
import com.jdcjk.view.MyRadioButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

import static com.baidu.navisdk.adapter.PackageUtil.getSdcardDir;
import static com.jdcjk.chat.OpenFireChatMessageListener.terminalClose;
import static com.jdcjk.chat.OpenFireChatMessageListener.updataWarningInfoNumber;
import static com.jdcjk.chat.OpenFireConnectionListener.restartLogin;

/**
 * Created by dong on 2016/12/2.
 * 车辆管理
 */
public class CarActivity extends BaseActivity<CarPresenter> implements View.OnClickListener, FragmentViewClickListener {
    private final String TAG = CarActivity.class.getSimpleName();
    private CarPresenter carPresenter;
    private boolean trafficEnable = false;
    private RadioGroup radioGroup;
    private MyRadioButton realtimeTrack_radioButton, trackPlay_radioButton, warmingInforadioButton, setting_radioButton;

    private FrameLayout bottomTabBdWarminInfo_fl;
    //未阅读报警信息数量textview
    private TextView carWarningInfoNumber_textView;
    //实时追踪和轨迹回放高德版Fragment
    private GaoDeRealTrackFragment gaoDeRealTrackFragment;
    //实时追踪和轨迹回放百度版Fragment
    private BaiDuRealTrackFragment baiDuRealTrackFragment;
    //报警信息Fragment
    private CarWarmingInfoFragment carWarmingInfoFragment;
    //车辆设置Fragment
    private CarSettingFragment carSettingFragment;
    //设备id
    private int terminalId;
    //时间选择dialog
    private ChoiceTimeDialog choiceTimeDialog;

    //loadingDialog
    private LoadingDialog loadingDialog;
    //重新登录dialog
    private AccountOfflineDialog accountOfflineDialog;

    //设置地图类型
    private String mapType = "GaoDe";


    @Override
    public int getLayoutId() {
        return R.layout.activity_car;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        //获得所设置的地图类型
        mapType = PreferencesUtils.getString(this, PreferencesUtils.MAP_TYPE);
        terminalId = getIntent().getExtras().getInt(Constant.TerminalInfo.terminalId);
        radioGroup = (RadioGroup) findViewById(R.id.car_bottom_tab_bar_rg);
        realtimeTrack_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_realtime_track);
        realtimeTrack_radioButton.setOnClickListener(this);
        trackPlay_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_track_play);
        trackPlay_radioButton.setOnClickListener(this);
        warmingInforadioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_warming_info);
//        warmingInforadioButton.setOnClickListener(this);
        setting_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_setting);
        setting_radioButton.setOnClickListener(this);
        carWarningInfoNumber_textView = (TextView) findViewById(R.id.car_warning_info_number_tv);
        bottomTabBdWarminInfo_fl = (FrameLayout) findViewById(R.id.bottom_tab_bd_warming_info_fl);
        bottomTabBdWarminInfo_fl.setOnClickListener(this);
        carWarmingInfoFragment = new CarWarmingInfoFragment();
        carWarmingInfoFragment.setFragmentViewClickListener(this);
        carSettingFragment = new CarSettingFragment();
        carSettingFragment.setFragmentViewClickListener(this);
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.TerminalInfo.terminalId, terminalId);
        if (mapType.equals("BaiDu")) {
            //如果设置的是百度地图
            baiDuRealTrackFragment = new BaiDuRealTrackFragment(GaoDeRealTrackFragment.REAL_TIME_TTACK);
            baiDuRealTrackFragment.setFragmentViewClickListener(this);
            baiDuRealTrackFragment.setArguments(bundle);
            addFragmentContent(baiDuRealTrackFragment, R.id.car_fragment, 1);
        } else {
            //否则为高德地图
            gaoDeRealTrackFragment = new GaoDeRealTrackFragment(GaoDeRealTrackFragment.REAL_TIME_TTACK);
            gaoDeRealTrackFragment.setFragmentViewClickListener(this);
            gaoDeRealTrackFragment.setArguments(bundle);
            addFragmentContent(gaoDeRealTrackFragment, R.id.car_fragment, 1);
        }
        carWarmingInfoFragment.setArguments(bundle);
        carSettingFragment.setArguments(bundle);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId == realtimeTrack_radioButton.getId()) {
                    if (mapType.equals("BaiDu")) {
                        switchDiffFragmentContent(baiDuRealTrackFragment, R.id.car_fragment, 1, false, false);
                        baiDuRealTrackFragment.changeFragmentType(GaoDeRealTrackFragment.REAL_TIME_TTACK);
                    } else {
                        switchDiffFragmentContent(gaoDeRealTrackFragment, R.id.car_fragment, 1, false, false);
                        gaoDeRealTrackFragment.changeFragmentType(GaoDeRealTrackFragment.REAL_TIME_TTACK);
                    }
                } else if (checkedId == trackPlay_radioButton.getId()) {
                    if (mapType.equals("BaiDu")) {
                        switchDiffFragmentContent(baiDuRealTrackFragment, R.id.car_fragment, 1, false, false);
                        baiDuRealTrackFragment.changeFragmentType(GaoDeRealTrackFragment.TRACK_PLAY_BACK);
                    } else {
                        switchDiffFragmentContent(gaoDeRealTrackFragment, R.id.car_fragment, 1, false, false);
                        gaoDeRealTrackFragment.changeFragmentType(GaoDeRealTrackFragment.TRACK_PLAY_BACK);
                    }
                } else if (checkedId == warmingInforadioButton.getId()) {

                } else if (checkedId == setting_radioButton.getId()) {
                    switchDiffFragmentContent(carSettingFragment, R.id.car_fragment, 4, false, false);
                }
            }
        });
        //初始化加载dialog
        loadingDialog = new LoadingDialog(this, R.style.dialogTheme);
        // 初始化EventBus
        EventBus.getDefault().register(this);
        //获取设备未查看报警信息数量
        carPresenter.getUnreadMsgNumForTerminalId(UrlAddress.GET_UNREAD_MSG_NUM_FOR_TERMINALID, String.valueOf(terminalId), new BasePresenter.OnUIThreadListener<String>() {
            @Override
            public void onBefore(Request request, int id) {

            }

            @Override
            public void onAfter(int id) {

            }

            @Override
            public void onError(Call call, Exception e, int i) {

            }

            @Override
            public void onResponse(String result) {
                int warningNumber = Integer.valueOf(result);
                //存储所有设备未查看报警信息数量
                PreferencesUtils.setInt(MainApp.getInstance(), PreferencesUtils.WARNING_INFO_NUMBER + terminalId, warningNumber);
                String warningNumberInfo = warningNumber > 99 ? "99+" : String.valueOf(warningNumber);
                //判断报警信息数量，为0的话不显示数量信息红点
                carWarningInfoNumber_textView.setVisibility(warningNumber == 0 ? View.INVISIBLE : View.VISIBLE);
                //存储报警信息值
                carWarningInfoNumber_textView.setText(warningNumberInfo);
            }
        });
    }

    @Override
    public CarPresenter bindPresenter() {
        carPresenter = new CarPresenter(getBaseContext());
        return carPresenter;
    }

    @Override
    public void onDestroy() {
        //解绑EventBus
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        //判断当前开启地图类型
        if (mapType.equals("GaoDe")) {
            //判断如果在进行回放，则停止
            if (gaoDeRealTrackFragment.isPlayBack) {
                gaoDeRealTrackFragment.playBack();
            }
            return;
        }
        //判断如果在进行回放，则停止
        if (baiDuRealTrackFragment.isPlayBack) {
            baiDuRealTrackFragment.playBack();
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bottom_tab_bd_realtime_track:
                selectRadioButton(1);
                break;
            case R.id.bottom_tab_bd_track_play:
                selectRadioButton(2);
                break;
            case R.id.bottom_tab_bd_warming_info_fl:
                selectRadioButton(3);
                switchDiffFragmentContent(carWarmingInfoFragment, R.id.car_fragment, 3, false, false);
                PreferencesUtils.setInt(CarActivity.this, PreferencesUtils.WARNING_INFO_NUMBER + terminalId, 0);
                carWarningInfoNumber_textView.setVisibility(View.INVISIBLE);
                //设置点击的button ID
                radioGroup.check(warmingInforadioButton.getId());
                break;
            case R.id.bottom_tab_bd_setting:
                selectRadioButton(4);
                break;
            case R.id.dialog_choice_time_determine_btn:
                //时间选择dialog确定按钮
                String startTime = choiceTimeDialog.getStartTime();
                String endTime = choiceTimeDialog.getEndTime();
                //判断地图类型，设置路径
                if (mapType.equals("GaoDe"))
                    gaoDeRealTrackFragment.initPathToday(terminalId, startTime, endTime);
                else
                    baiDuRealTrackFragment.initPathToday(terminalId, startTime, endTime);
                choiceTimeDialog.dismiss();
                break;
        }
    }

    /**
     * 根据buttonTag设置RadioButton状态，从底部栏左到右一次为1,2,3,4
     *
     * @param buttonTag
     */
    public void selectRadioButton(int buttonTag) {
        realtimeTrack_radioButton.setChecked(buttonTag == 1);
        trackPlay_radioButton.setChecked(buttonTag == 2);
        warmingInforadioButton.setChecked(buttonTag == 3);
        setting_radioButton.setChecked(buttonTag == 4);
    }


    @Override
    public void onFragmentViewClickListener(View v) {
        switch (v.getId()) {
            case R.id.fragment_real_time_map_type_btn:
                if (mapType.equals("GaoDe")) {
                    if (gaoDeRealTrackFragment.getAMap().getMapType() == AMap.MAP_TYPE_NORMAL) {
                        gaoDeRealTrackFragment.getAMap().setMapType(AMap.MAP_TYPE_SATELLITE);
                        gaoDeRealTrackFragment.getMapTypeImageView().setImageResource(R.drawable.fragment_car_monitor_change_to_normal);
                    } else if (gaoDeRealTrackFragment.getAMap().getMapType() == AMap.MAP_TYPE_SATELLITE) {
                        gaoDeRealTrackFragment.getAMap().setMapType(AMap.MAP_TYPE_NORMAL);
                        gaoDeRealTrackFragment.getMapTypeImageView().setImageResource(R.drawable.fragment_car_monitor_change_to_satellite);
                    }
                } else {
                    if (baiDuRealTrackFragment.getBDMap().getMapType() == AMap.MAP_TYPE_NORMAL) {
                        baiDuRealTrackFragment.getBDMap().setMapType(AMap.MAP_TYPE_SATELLITE);
                        baiDuRealTrackFragment.getMapTypeImageView().setImageResource(R.drawable.fragment_car_monitor_change_to_normal);
                    } else if (baiDuRealTrackFragment.getBDMap().getMapType() == AMap.MAP_TYPE_SATELLITE) {
                        baiDuRealTrackFragment.getBDMap().setMapType(AMap.MAP_TYPE_NORMAL);
                        baiDuRealTrackFragment.getMapTypeImageView().setImageResource(R.drawable.fragment_car_monitor_change_to_satellite);
                    }
                }
                break;
            case R.id.fragment_real_time_traffic_btn:
                //交通图显示
                if (mapType.equals("GaoDe")) {
                    gaoDeRealTrackFragment.setMapTraffic();
                } else {
                    baiDuRealTrackFragment.setMapTraffic();
                }
                break;
            case R.id.car_navi_find_btn:
                //寻车导航
                if (mapType.equals("GaoDe")) {
                    CarListInfo carListInfo = gaoDeRealTrackFragment.getCarListInfo();
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent(this, GDCarNavigationFindActivity.class);
                    bundle.putString(Constant.TerminalInfo.terminalId, carListInfo.getTerminalNumber());
                    bundle.putDouble(Constant.TerminalInfo.terminalLatitude, carListInfo.getLatitude());
                    bundle.putDouble(Constant.TerminalInfo.terminalLongitude, carListInfo.getLongitude());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    if (initDirs()) {
                        initNavi();
                    }
                }

                break;
            case R.id.change_position_btn:
                //位置切换
                if (mapType.equals("GaoDe")) {
                    gaoDeRealTrackFragment.changePosition();
                } else {
                    baiDuRealTrackFragment.changePosition();
                }
                break;
            case R.id.back_btn:
                //回退按钮
                finish();
                break;
            case R.id.play_back_btn:
                //回放按钮
                if (mapType.equals("GaoDe")) {
                    gaoDeRealTrackFragment.playBack();
                } else {
                    baiDuRealTrackFragment.playBack();
                }
                break;
            case R.id.time_choice_btn:
                //时间选择按钮
                choiceTimeDialog = new ChoiceTimeDialog();
                choiceTimeDialog.setOnClickListener(this);
                choiceTimeDialog.show(getFragmentManager(), "");
                //如果正在回放则暂停
                if (mapType.equals("GaoDe") && gaoDeRealTrackFragment.isPlayBack) {
                    gaoDeRealTrackFragment.playBack();
                } else if (mapType.equals("BaiDu") && baiDuRealTrackFragment.isPlayBack) {
                    baiDuRealTrackFragment.playBack();
                }

                break;
            case R.id.trajectory_correction_btn:
                //轨迹纠偏按钮
                if (mapType.equals("GaoDe")) {
                    gaoDeRealTrackFragment.trajectoryCorrection();
                } else {
                    baiDuRealTrackFragment.trajectoryCorrection();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            //获得设备信息返回设局
            Bundle bundle = data.getExtras();
            String displayid = bundle.getString(Constant.TerminalInfo.displayid);//设备名称
            String carId = bundle.getString(Constant.TerminalInfo.carId);//设备车牌号
            String cardNum = bundle.getString(Constant.TerminalInfo.cardNum);//设备手机卡号
            //更新当前设备信息
            if (mapType.equals("GaoDe")) {
                gaoDeRealTrackFragment.updataCarListInfo(displayid, carId, cardNum);
            } else {
                baiDuRealTrackFragment.updataCarListInfo(displayid, carId, cardNum);
            }
        }
    }

    /**
     * 接收收到的位置信息
     *
     * @param locationBean 位置信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLatlngByEventBus(MessageBean.LocationBean locationBean) {
        //如果是当前车辆的位置信息并且实时追踪是否开启，则追踪
        if (locationBean.getTerminalId() == terminalId) {
            //绘制对应位置信息
            if (mapType.equals("GaoDe") && gaoDeRealTrackFragment.isRealTimeTrack) {
                gaoDeRealTrackFragment.drawRealTimePath(locationBean);
            } else if (mapType.equals("BaiDu") && baiDuRealTrackFragment.isRealTimeTrack) {
                baiDuRealTrackFragment.drawRealTimePath(locationBean);
            }
        }
    }

    /**
     * 接收到的设备状态信息
     *
     * @param terminalStatusBean 位置信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLatlngByEventBus(MessageBean.TerminalStatusBean terminalStatusBean) {
        //如果是当前车辆的位置信息，则更新车辆数据
        if (terminalStatusBean.getId() == terminalId) {
            if (mapType.equals("GaoDe")) {
                gaoDeRealTrackFragment.updataCarInfo(terminalStatusBean);
            } else {
                baiDuRealTrackFragment.updataCarInfo(terminalStatusBean);
            }
        }
    }

    /**
     * 接收设备关闭信息
     *
     * @param message 设备信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLatlngByEvenBus(String message) {
        if (message.equals(terminalClose)) {
            //如果是关闭设备
            if (mapType.equals("GaoDe")) {
                gaoDeRealTrackFragment.closeTermianl();
            } else {
                baiDuRealTrackFragment.closeTermianl();
            }
            return;
        }
        if (message.equals(restartLogin)) {
            //判断dialog是否已经存在并在最上层，防止重复创建
            if (accountOfflineDialog == null || accountOfflineDialog.isHidden()) {
                //如果是弹出重新登录dialog
                accountOfflineDialog = new AccountOfflineDialog();
                accountOfflineDialog.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //重新登录按钮监听
                        //及时通讯初始化
                        MainApp.getInstance().initXMPPTCPConnection();
                        accountOfflineDialog.dismiss();
                        accountOfflineDialog = null;
                    }
                });
                accountOfflineDialog.show(getFragmentManager(), "");
            }
        }
        //判断是否是更新报警信息数量,以及是否是本车辆的报警信息
        if (message.contains(updataWarningInfoNumber) && message.contains(String.valueOf(terminalId))) {
            //是的话更新未阅读报警信息数量
            int warningInfoNumber = PreferencesUtils.getInt(this, PreferencesUtils.WARNING_INFO_NUMBER + terminalId);
            String carWarningNumber = warningInfoNumber > 99 ? "99+" : String.valueOf(warningInfoNumber);
            carWarningInfoNumber_textView.setText(String.valueOf(carWarningNumber));
            //根据信息数量设置现不显示未阅读报警信息数量的红点
            carWarningInfoNumber_textView.setVisibility(warningInfoNumber == 0 ? View.INVISIBLE : View.VISIBLE);
        }
    }

    /*****************************百度导航相关方法*****************************/

    /**
     * App在SD卡中的目录名
     */
    private static final String APP_FOLDER_NAME = "jdcjk";
    /**
     * SD卡的路径
     */
    private String mSDCardPath = null;
    /**
     * 对于导航模块有两种方式来实现发起导航。 1：使用通用接口来实现 2：使用传统接口来实现
     */
    // 是否使用通用接口
    private boolean useCommonInterface = true;

    /**
     * 初始化SD卡，在SD卡路径下新建文件夹：App目录名，文件中包含了很多东西，比如log、cache等等
     *
     * @return
     */
    private boolean initDirs() {
        mSDCardPath = getSdcardDir();
        if (mSDCardPath == null) {
            return false;
        }
        File f = new File(mSDCardPath, APP_FOLDER_NAME);
        if (!f.exists()) {
            try {
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private String authinfo = null;

    private void initNavi() {
        loadingDialog.setTextView(R.string.baidu_navi_init);
        loadingDialog.show();
        BaiduNaviManager.getInstance().init(CarActivity.this, mSDCardPath, APP_FOLDER_NAME, new BaiduNaviManager.NaviInitListener() {
            @Override
            public void onAuthResult(int status, String msg) {
                if (0 == status) {
                    authinfo = "key校验成功!";
                } else {
                    authinfo = "key校验失败, " + msg;
                }
            }

            public void initSuccess() {
//                Toast.makeText(CarActivity.this, "百度导航引擎初始化成功", Toast.LENGTH_SHORT).show();
                initSetting();
                LatLng personalLatLng = baiDuRealTrackFragment.getPersonalLatLng();
                LatLng carLatLng = baiDuRealTrackFragment.getCarLatLng();
                routeplanToNavi(personalLatLng, carLatLng, BNRoutePlanNode.CoordinateType.BD09LL);
                loadingDialog.dismiss();
            }

            public void initStart() {
//                Toast.makeText(CarActivity.this, "百度导航引擎初始化开始", Toast.LENGTH_SHORT).show();
            }

            public void initFailed() {
//                Toast.makeText(CarActivity.this, "百度导航引擎初始化失败", Toast.LENGTH_SHORT).show();
                loadingDialog.dismiss();
            }
        }, null, ttsHandler, null);
    }

    private void initSetting() {
        // 设置是否双屏显示
        BNaviSettingManager.setShowTotalRoadConditionBar(BNaviSettingManager.PreViewRoadCondition.ROAD_CONDITION_BAR_SHOW_ON);
        // 设置导航播报模式
        BNaviSettingManager.setVoiceMode(BNaviSettingManager.VoiceMode.Veteran);
        // 是否开启路况
        BNaviSettingManager.setRealRoadCondition(BNaviSettingManager.RealRoadCondition.NAVI_ITS_ON);
    }

    /**
     * 内部TTS播报状态回传handler
     */
    private Handler ttsHandler = new Handler() {
        public void handleMessage(Message msg) {
            int type = msg.what;
            switch (type) {
                case BaiduNaviManager.TTSPlayMsgType.PLAY_START_MSG: {
                    break;
                }
                case BaiduNaviManager.TTSPlayMsgType.PLAY_END_MSG: {
                    break;
                }
                default:
                    break;
            }
        }
    };

    private void routeplanToNavi(LatLng personalLatLng, LatLng carLatLng, BNRoutePlanNode.CoordinateType coType) {
        BNRoutePlanNode sNode = null;
        BNRoutePlanNode eNode = null;
        switch (coType) {
            case GCJ02: {
                sNode = new BNRoutePlanNode(personalLatLng.longitude, personalLatLng.latitude, "当前位置", null, coType);
                eNode = new BNRoutePlanNode(carLatLng.longitude, carLatLng.latitude, "车辆位置", null, coType);
                break;
            }
            case WGS84: {
                sNode = new BNRoutePlanNode(personalLatLng.longitude, personalLatLng.latitude, "当前位置", null, coType);
                eNode = new BNRoutePlanNode(carLatLng.longitude, carLatLng.latitude, "车辆位置", null, coType);
                break;
            }
            case BD09_MC: {
                sNode = new BNRoutePlanNode(personalLatLng.longitude, personalLatLng.latitude, "当前位置", null, coType);
                eNode = new BNRoutePlanNode(carLatLng.longitude, carLatLng.latitude, "车辆位置", null, coType);
                break;
            }
            case BD09LL: {
                sNode = new BNRoutePlanNode(personalLatLng.longitude, personalLatLng.latitude, "当前位置", null, coType);
                eNode = new BNRoutePlanNode(carLatLng.longitude, carLatLng.latitude, "车辆位置", null, coType);
                break;
            }
        }
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new DemoRoutePlanListener(sNode));
        }
    }

    /**
     * 导航回调监听器
     */
    public class DemoRoutePlanListener implements BaiduNaviManager.RoutePlanListener {
        private BNRoutePlanNode mBNRoutePlanNode = null;

        public DemoRoutePlanListener(BNRoutePlanNode node) {
            mBNRoutePlanNode = node;
        }

        @Override
        public void onJumpToNavigator() {
            Intent intent = new Intent(CarActivity.this, BDCarNavigationFindActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("routePlanNode", mBNRoutePlanNode);
            intent.putExtras(bundle);
            startActivity(intent);
        }

        @Override
        public void onRoutePlanFailed() {
            Toast.makeText(CarActivity.this, R.string.baidu_calculating_path_fail, Toast.LENGTH_SHORT).show();
            //算路失败时，启动高德地图导航
            CarListInfo carListInfo = baiDuRealTrackFragment.getCarListInfo();
            Bundle bundle = new Bundle();
            Intent intent = new Intent(CarActivity.this, GDCarNavigationFindActivity.class);
            bundle.putString(Constant.TerminalInfo.terminalId, carListInfo.getTerminalNumber());
            LatLng LatLng = new LatLng(carListInfo.getLatitude(), carListInfo.getLongitude());
            //将百度地图坐标转成高德地图坐标
            LatLng= BaiDuMapUtil.bd09_To_Gcj02(LatLng.latitude,LatLng.longitude);
            bundle.putDouble(Constant.TerminalInfo.terminalLatitude, LatLng.latitude);
            bundle.putDouble(Constant.TerminalInfo.terminalLongitude, LatLng.longitude);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
}
